#include "stdafx.h"
#include "net/RawSocket.hpp"
#include "net/RawSocketImpl.hpp"

NET_NAMESPACE_BEGIN

CRawSocket::CRawSocket() : CSocket(new CRawSocketImpl)
{
}

CRawSocket::CRawSocket(CIPAddress::Family family, int proto) : CSocket(new CRawSocketImpl(family, proto))
{
}

CRawSocket::CRawSocket(const CSocketAddress& address, bool reuseAddress) : CSocket(new CRawSocketImpl(address.family()))
{
	bind(address, reuseAddress);
}

CRawSocket::CRawSocket(const CSocket& socket) : CSocket(socket)
{
	if (!dynamic_cast<CRawSocketImpl*>(impl()))
		throw InvalidArgumentException("Cannot assign incompatible socket");
}

CRawSocket::CRawSocket(CSocketImpl* pImpl) : CSocket(pImpl)
{
	if (!dynamic_cast<CRawSocketImpl*>(impl()))
		throw InvalidArgumentException("Cannot assign incompatible socket");
}

CRawSocket::~CRawSocket()
{
}

CRawSocket& CRawSocket::operator = (const CSocket& socket)
{
	if (dynamic_cast<CRawSocketImpl*>(socket.impl()))
		CSocket::operator = (socket);
	else
		throw InvalidArgumentException("Cannot assign incompatible socket");
	return *this;
}

void CRawSocket::connect(const CSocketAddress& address)
{
	impl()->connect(address);
}

void CRawSocket::bind(const CSocketAddress& address, bool reuseAddress)
{
	impl()->bind(address, reuseAddress);
}

int CRawSocket::sendBytes(const void* buffer, int length, int flags)
{
	return impl()->sendBytes(buffer, length, flags);
}

int CRawSocket::receiveBytes(void* buffer, int length, int flags)
{
	return impl()->receiveBytes(buffer, length, flags);
}

int CRawSocket::sendTo(const void* buffer, int length, const CSocketAddress& address, int flags)
{
	return impl()->sendTo(buffer, length, address, flags);
}

int CRawSocket::receiveFrom(void* buffer, int length, CSocketAddress& address, int flags)
{
	return impl()->receiveFrom(buffer, length, address, flags);
}


NET_NAMESPACE_END
